package com.learn.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.ssm.entity.Emp;
import com.learn.ssm.mapper.EmpMapper;
import com.learn.ssm.service.api.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<Emp> selectAll() {
        return empMapper.selectAll();
    }

    @Override
    public PageInfo<Emp> getPageInfo(Integer pageNo) {

        int pageSize = 5;

        PageHelper.startPage(pageNo, pageSize);

        List<Emp> empList = empMapper.selectAll();

        return new PageInfo<>(empList);
    }

    @Override
    @Transactional(
            readOnly = false,
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = Exception.class)
    public void removeByEmpId(Integer empId) {

        empMapper.deleteByPrimaryKey(empId);
    }

    @Override
    public Emp getEmpById(Integer empId) {
        return empMapper.queryEmpByPrimaryKey(empId);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        empMapper.insertEmp(emp);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        empMapper.updateEmp(emp);
    }
}
